/*
 * 创建日期 2005-3-20
 */
package com.maxjay.common.exception;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <p>
 * Title: 框架
 * </p>
 * <p>
 * Description: 错误消息处理类
 * </p>
 * <p>
 * Copyright: Copyright (c) 2005
 * </p>
 * <p>
 * Company: 公司
 * </p>
 * 
 * @author 郑美旺
 * @version 1.0
 */
public class ErrorMessage {

	private static Log log = LogFactory.getLog(ErrorMessage.class);

	public static final String DELIMITER = ":";

	public static Properties errorMessages = new Properties();

	private static Locale locale = null;

	/**
	 * 根据错误代码得到对应的错误消息
	 * 
	 * @param code
	 *            错误代码
	 * @return String 错误消息
	 */
	private static String getErrorMessage(int code) {
		String sCode = Integer.toString(code);
		if (locale != null) {
			sCode += "_" + locale.getLanguage();
		}
		String errMessage = errorMessages.getProperty(sCode);

		if (errMessage == null) {
			reload();
			errMessage = errorMessages.getProperty(sCode);
		}
		// 如果对应语言没找到,则找默认代码对应消息
		if (errMessage == null && locale != null) {
			errMessage = errorMessages.getProperty(Integer.toString(code));
		}

		return convertToUnicode(errMessage);
	}

	/**
	 * 根据错误代码得到替换后的错误消息
	 * 
	 * @param errorCode
	 *            错误代码
	 * @param lstPattern
	 *            替换Pattern
	 * @return String 经过替换后的错误消息
	 */
	public static String getErrorMessage(int errorCode, String[] lstPattern) {
		String errorMessage = getErrorMessage(errorCode);

		if (errorMessage != null) {
			if (checkLevle(errorMessage)) {
				errorMessage = errorMessage.substring(2);
			}

			if (lstPattern != null) {
				for (int i = 0; i < lstPattern.length; i++) {
					errorMessage = errorMessage.replaceAll("%" + (i + 1), lstPattern[i]);
				}
			}
		}

		return errorMessage;
	}

	/**
	 * 检查是否定义了错误级别
	 * 
	 * @param errorMessage
	 *            错误消息
	 * @return boolean 如果定义了错误级别，则返回true，否则返回false
	 */
	private static boolean checkLevle(String errorMessage) {
		if (errorMessage.length() >= 2) {
			String levelCode = errorMessage.substring(0, 1);
			String delimiter = errorMessage.substring(1, 2);

			try {
				int level = Integer.parseInt(levelCode);

				if (level >= 0 && level <= 5 && delimiter.equals(DELIMITER)) {
					return true;
				}
			} catch (NumberFormatException nfe) {
			}
		}

		return false;
	}

	/**
	 * 返回错误代码对应的错误级别
	 * 
	 * @param code
	 *            错误代码
	 * @return int 错误级别，如果未定义，则返回ErrorLevel.NONE
	 */
	public static int getErrorLevel(int code) {
		String errorMessage = getErrorMessage(code);

		if (errorMessage != null) {
			if (checkLevle(errorMessage)) {
				return Integer.parseInt(errorMessage.substring(0, 1));
			}
		}

		return ErrorLevel.NONE;
	}

	/**
	 * 装载配置文件
	 * 
	 */
	private static synchronized void reload() {
		InputStream is = ErrorMessage.class.getResourceAsStream("ErrorCode.properties");

		try {
			errorMessages.load(is);
		} catch (Exception e) {
			log.debug("读取配置文件时发生以下错误： " + e.getMessage());
		} finally {
			try {
				if (is != null) {
					is.close();
				}
			} catch (Exception e) {
			}
		}
	}

	private static String convertToUnicode(String source) {
		String des = null;
		if (source != null) {
			try {
				des = new String(source.getBytes("iso8859-1"), "utf-8");
			} catch (UnsupportedEncodingException e) {
				des = "";
			}
			return des;
		} else
			return "";
	}

	/**
	 * 错误消息语言本地化
	 * @param locale
	 */
	public static void setLocale(Locale locale) {
		ErrorMessage.locale = locale;
	}
}
